package voja.android.mysql;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.widget.Toast;

public class Prefs extends PreferenceActivity {
	
	protected void onCreate(Bundle instance) {
		super.onCreate(instance);
        addPreferencesFromResource(R.xml.preferences);
        
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
        sp.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener() {
			public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
				String host = sharedPreferences.getString("cHost", "localhost");
				String port = sharedPreferences.getString("cPort", "3306");
				String db = sharedPreferences.getString("cDatabase", "test");
				String user = sharedPreferences.getString("cUsername", "root");
				String pass = sharedPreferences.getString("cPassword", "");
				
				try {
					Mysql.updateConnection(host, port, db, user, pass);
					Toast.makeText(getBaseContext(),"Settings have been changed.", Toast.LENGTH_LONG).show();
				} catch(Exception e) {
					Toast.makeText(getBaseContext(),e.getMessage(), Toast.LENGTH_LONG).show();
				}
			}
		});
	}	
}
